package com.migo.utils;

import com.migo.entity.Mail;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;

import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;



public class MailUtil {


    public static boolean send(Mail mail, List<InternetAddress> list) {
        // 发送email
        HtmlEmail email = new HtmlEmail();
        try {
//            email.setSmtpPort(0);
            // 这里是SMTP发送服务器的名字：163的如下："smtp.163.com"
            email.setHostName(mail.getHost());
            email.setSSL(Boolean.TRUE);
            email.setSslSmtpPort("465");
            // 字符编码集的设置
            email.setCharset(Mail.ENCODEING);
            // 收件人的邮箱
//			email.addTo(mail.getReceiver());
//			List<InternetAddress> list=new ArrayList<InternetAddress>();
////			list.add("licunshan@163.com");
//			list.add(new InternetAddress("paik.li@powervisions.com.cn"));
//			list.add(new InternetAddress("530867236@qq.com"));
//			list.add(new InternetAddress("licunshan@163.com"));
            email.setTo(list);
            // 发送人的邮箱
            email.setFrom(mail.getSender(), mail.getName());
            // 如果需要认证信息的话，设置认证：用户名-密码。分别为发件人在邮件服务器上的注册名称和密码
            email.setAuthentication(mail.getUsername(), mail.getPassword());
            // 要发送的邮件主题
            email.setSubject(mail.getSubject());
            // 要发送的信息，由于使用了HtmlEmail，可以在邮件内容中使用HTML标签
            email.setHtmlMsg(mail.getMessage());
            // 发送
            email.send();
            return true;
        } catch (EmailException e) {
            e.printStackTrace();
            return false;
        }
    }

    public static void main(String[] args) throws AddressException {
        Mail mail = new Mail();
        mail.setHost("smtp.powervision.me"); // smtp.powervision.me ip 211.150.65.81 设置邮件服务器,如果不用163的,自己找找看相关的
        mail.setSender("noreply@powervision.me");
        mail.setUsername("noreply@powervision.me"); // 登录账号,一般都是和邮箱名一样吧
        mail.setPassword("q1W2E3R4T5"); // 发件人邮箱的登录密码
        mail.setSubject("awdaw");//邮件主题
        mail.setName("awd");//发件人昵称
        String msg = getMessage("您的验证码是：2345678");
        mail.setMessage(msg);
        List<InternetAddress> list = new ArrayList<InternetAddress>();
        list.add(new InternetAddress("paik.li@powervision.me"));
        boolean flag = new MailUtil().send(mail, list);
        System.out.println(flag);
//        return flag;
    }

    public static boolean sendUserMail(String subject, String message, List<InternetAddress> list, String name) {
        Mail mail = new Mail();
        mail.setHost("smtp.powervision.me"); // 设置邮件服务器,如果不用163的,自己找找看相关的
        mail.setSender("noreply@powervision.me");
//		mail.setReceiver(useremail); // 接收人
        mail.setUsername("noreply@powervision.me"); // 登录账号,一般都是和邮箱名一样吧
        mail.setPassword("q1W2E3R4T5"); // 发件人邮箱的登录密码
        mail.setSubject(subject);//邮件主题
        mail.setName(name);//发件人昵称
        mail.setMessage(message);
        ExecutorService threadPool = ThreadPoolUtils.getThreadPool();
        threadPool.execute(new MailUtilRunable(mail, list));
        return true;
//
//        boolean flag = new MailUtil().send(mail, list);
//        return flag;
    }

    public static String getMessage(String massage) {
        String result = "<!DOCTYPE html>    "
                + "<html lang=\"zh-CN\">    "
                + "<head>    "
                + "    <meta charset=\"utf-8\">    "
                + "    <meta name=\"viewport\" content=\"width=device-width ,initial-scale=1.0\">    "
                + "    <title>Powervision </title>    "
                + "    <style>    "
                + "        body {    "
                + "            background-color: #333333;    "
                + "            color: #FFFFFF;    "
                + "            font-family: Arial;    "
                + "            font-size: 24px;    "
                + "            font-weight: bold;    "
                + "        }    "
                + "    "
                + "        .titleclass {    "
                + "            margin-left: auto;    "
                + "            margin-right: auto;    "
                + "            top: 50px;    "
                + "        }    "
                + "    "
                + "        .contentclass {    "
                + "            margin-left: auto;    "
                + "            margin-right: auto;    "
                + "            margin-top: 20px;    "
                + "            margin-left:40px;    "
                + "    "
                + "        }    "
                + "    "
                + "        .imageclass {    "
                + "            float: left;    "
                + "            margin: 2px;    "
                + "    "
                + "        }    "
                + "    "
                + "        .footclass {    "
                + "            margin-left: auto;    "
                + "            margin-right: auto;    "
                + "            bottom: 50px;    "
                + "        }    "
                + "        .textClass {    "
                + "            margin-left: auto;    "
                + "            margin-right: auto;    "
                + "            margin-left:40px;    "
                + "        }    "
                + "    </style>    "
                + "</head>    "
                + "    "
                + "<body>    "
                + "    <div class=\"titleclass\">    "
                + "        <img width=\"350px\" height=\"150px\" src=\"http://www.powervision.cn/disc/images/LOGO-BLACK.png\" />    "
                + "    </div>    "
                + "    <div class=\"textClass\">    "
                + "            " + massage + "   "
                + "    </div>    "
                + "    <div class=\"contentclass\">    "
                + "        <a href=\"http://www.powervision.me/\"> <img src=\"http://gcsbucket.oss-cn-hongkong.aliyuncs.com/gcsapp/gcsemail/raybg.jpg?x-oss-process=image/resize,m_fill,h_450,w_650\" /></a>     "
                + "        <a href=\"http://www.powervision.me/\"> <img src=\"http://gcsbucket.oss-cn-hongkong.aliyuncs.com/gcsapp/gcsemail/powereye_bg1.jpg?x-oss-process=image/resize,m_fill,h_250,w_300\" /></a>     "
                + "        <a href=\"http://www.powervision.me/\">   <img src=\"http://gcsbucket.oss-cn-hongkong.aliyuncs.com/gcsapp/gcsemail/tu_5.jpg?x-oss-process=image/resize,m_fill,h_250,w_300\" /></a>     "
                + "        <a href=\"http://www.powervision.me/\">  <img src=\"http://gcsbucket.oss-cn-hongkong.aliyuncs.com/gcsapp/gcsemail/powerbee_section1Bg.jpg?x-oss-process=image/resize,m_fill,h_250,w_650\" /></a>     "
                + "    </div>    "
                + "</body>    "
                + "</html>";
        return result;

    }
}
